/**
 * http配置
 */
// 引入axios
import axios from 'axios'
import router from '@/router'
// 超时时间
axios.defaults.timeout = 30 * 1000

// http请求拦截器
axios.interceptors.request.use(config => {
  config.headers.axiosRequested = "XMLHttpRequest"
  if (localStorage.getItem("token")) {
    config.headers.token = localStorage.getItem("token")
  }
  return config
}, error => {
  return Promise.reject(error)
})

// http响应拦截器
axios.interceptors.response.use(response => {
  return response
}, error => {
  if (error.response) {
    switch (error.response.status) {
      case 302:
      case 403:
      case 504:
      case 500:
        // 返回 401 清除token信息并跳转到登录页面
        router.replace({
          path: 'login',
          query: {redirect: router.currentRoute.fullPath}
        })
    }
  }
  return Promise.reject(error)
})

export default axios
